<?php
namespace app\models;
use yii;
use yii\db\ActiveRecord;
use yii\db\Query;
class Slide extends ActiveRecord
{
    /*自动判断 新建或更新*/
    public function autoUpdate($name,$url)
    {
        $info=$this->getoneByName($name);
        if($info)
        {
            return $this->updateByName($name,$url);
        }
        else
        {
            return $this->createNew($name,$url);
        }

    }

    /*根据name更新*/
    public function updateByName($name,$url)
    {
        $where=array(
            'name'=>$name
        );
        $up_data=array(
            'url'=>$url
        );
        return Yii::$app->db->createCommand()->update('slide',$up_data,$where)->execute();
    }

    /*新建*/
    public function createNew($name,$url)
    {
        $in_data=array(
            'name'=>$name,
            'url'=>$url
        );
        return Yii::$app->db->createCommand()->insert('slide',$in_data)->execute();
    }

    /*根据name 获取单条*/
    public function getoneByName($name)
    {
        $where=array(
            'name'=>$name
        );
        $info=(new Query())
            ->from('slide')
            ->where($where)
            ->one();
        return $info;
    }

    /*获取全部列表*/
    public function getlistAll()
    {
        $info = (new Query())
            ->from('slide')
            ->limit(3)
            ->all();
        return $info;
    }


}